<template>
  <van-empty image="network" :description="error" />
</template>

<script>
// import { getWxUserInfo } from '@/api/index.js'
import { getCurUserInfo } from '@/api/user'
import { getUserDeptList } from '@/api/rectify'
export default {
  name: 'WxAuth',
  data () {
    return {
      error: '登录认证，请稍候...'
    }
  },
  created () {
    if (!this.getQueryString('code') && !this.$store.state.authInfo.user) {
      // 南网elink认证链接，需要修改appid
      let url = `https://app.csg.cn:8801/connect/oauth2/authorize?appid=wl3709e1de3c&redirect_uri=https://app.ehv.csg.cn:41888/aj/index.html&response_type=code&scope=snsapi_userinfo&agentid=1000085#wechat_redirect`
     
      // 企业微信认证链接，需要修改appid
      // let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wwda840de5a6158205&redirect_uri=${encodeURIComponent(location.href)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect&connect_redirect=1`
      // let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww3eb468697931b013&redirect_uri=${encodeURIComponent(location.href)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect&connect_redirect=1`
      this.error = '进入eLink授权'
      console.debug('微信授权:', '第一次进入')
      window.location.href = url
    } else if (!this.$store.state.authInfo.user) {
      console.debug('微信授权:', '第二次进入且路径中存在code')
      let code = this.getUrlPara('code')
      // console.log('code = ' + code)
      this.error = '获取eLink当前登录用户信息'
      console.log('开始获取微信客户信息---------------------------------------')
      let _this = this
      // 这里只能通过后端服务获取openID
      getCurUserInfo(code).then(res => {
        let { data } = res
        console.log('获取微信用户信息：', data)
        if (this.$response.SUCCESS === data[this.$response.CODE]) {
          let user = data[this.$response.DATA]
          user.rectify = []
          this.$store.commit('authInfo/setUserInfo', user)
          this.error = '获取eLink用户身份成功'
          // let that = this
          // 增加获取问题整改身份部门列表
          getUserDeptList(user.userid).then(ret => {
            let { retCode, retData } = ret.data
            if (retCode === '0000') {
              user.rectify = retData
              _this.error = '获取问题整改身份部门列表成功' + user.rectify
              _this.$store.commit('authInfo/setUserInfo', user)
              _this.$router.push({ path: '/' })
            } else {
              console.log(retData[_this.$response.MESSAGE])
              _this.error = retData[_this.$response.MESSAGE]
              _this.$toast.fail(retData[_this.$response.MESSAGE])
            }
          }, err => {
            console.log('获取用户信息失败')
            _this.error = err.toString()
            _this.$notify('服务调用失败：' + err.toString())
          })
        } else {
          console.log(data[this.$response.MESSAGE])
          this.error = data[this.$response.MESSAGE]
          this.$toast.fail(data[this.$response.MESSAGE])
        }
      }, errFunc => {
        // 服务请求通信错误
        console.log('获取微信用户信息失败')
        this.error = errFunc.toString()
        this.$notify('服务调用失败：' + errFunc.toString())
      })
    }
  },
  methods: {
    getQueryString (strName) {
      let reg = new RegExp('(^|&)' + strName + '=([^&]*)(&|$)', 'i')
      let r = window.location.search.substr(1).match(reg)
      if (r != null) {
        return unescape(r[2])
      }
      return null
    },
    getUrlPara (paraName) {
      let paras = window.location.search
      if (paras.indexOf(paraName) > -1) {
        let paraValue = paras.substr(paras.indexOf(paraName), paras.length)
        if (paraValue.indexOf('&') > -1) {
          paraValue = paraValue.substr(0, paraValue.indexOf('&'))
          paraValue = paraValue.replace(paraName + '=', '')
          return paraValue
        }
        return ''
      }
    }
  }
}
</script>

<style scoped>

</style>
